<?php
namespace app\services\admin;

use app\models\Permission;
use app\models\PermissionGroup;

class PermissionService
{
    protected $permission;
    protected $permissionGroup;

    public function __construct(Permission $permission, PermissionGroup $permissionGroup)
    {
        $this->permission = $permission;
        $this->permissionGroup = $permissionGroup;
    }

    public function getPermission($id)
    {
        $res = $this->permission->field('name, auth, group_id, status')->where('id', $id)->find();

        if ($res) {
            $result = $res->toArray();
            $result['status'] = $res->getData('status');
            return $result;
        }
        return [];
    }

    public function getPermissionList($status, $search, $size = 15)
    {
        $res = $this->permission
            ->with('permissionGroup')
            ->withSearch(['name', 'status'], ['name' => $search, 'status' => $status])
            ->order('id', 'desc')
            ->paginate($size);
        if ($res) {
            $pageInfo = $res->toArray();
            /** 由于vue 中要int类型，框架输出的current_page是字符串类型，造成导航页码循环有问题 */
            $pageInfo['current_page'] = intval($pageInfo['current_page']);
            return $pageInfo;
        }

        return [];
    }

    public function getPermissionToSelect()
    {
        $res = $this->permission->order('id', 'asc')->select();
        $tmp = [];
        if (! $res->isEmpty()) {
            $res->each(function($item) use (& $tmp) {
                $tmp[] = ['id' => $item['id'], 'name' => $item['name']];
            });
        }
        return $tmp;
    }

    public function save($data)
    {
        if ($this->permission->allowField(['name', 'group_id', 'auth', 'status'])->save($data)) {
            return [true, '保存完成'];
        }
        return [false, '未知错误'];
    }

    public function modify($data)
    {
        $res = $this->permission->find($data['id']);

        if ($res) {
            $res->name = $data['name'];
            $res->auth = $data['auth'];
            $res->group_id = $data['group_id'];
            $res->status = $data['status'];

            if ($res->force()->save()) {
                return [true, '保存完成'];
            }
        }
        return [false, '未知错误'];
    }

    public function del($id)
    {
        $res = $this->permission->where('id', $id)->delete();

        if ($res) {
            return true;
        }

        return false;
    }

    public function getPermissionGroup($id)
    {
        $res = $this->permissionGroup->field('name, status')->where('id', $id)->find();

        if ($res) {
            $result = $res->toArray();
            $result['status'] = $res->getData('status');
            return $result;
        }
        return [];
    }

    public function getPermissionGroupList($status, $search, $size = 15)
    {
        $res = $this->permissionGroup
            ->withSearch(['name', 'status'], ['name' => $search, 'status' => $status])
            ->order('id', 'desc')
            ->paginate($size);
        if ($res) {
            $pageInfo = $res->toArray();
            /** 由于vue 中要int类型，框架输出的current_page是字符串类型，造成导航页码循环有问题 */
            $pageInfo['current_page'] = intval($pageInfo['current_page']);
            return $pageInfo;
        }

        return [];
    }

    public function groupSave($data)
    {
        if ($this->permissionGroup->allowField(['name', 'status'])->save($data)) {
            return [true, '保存完成'];
        }
        return [false, '未知错误'];
    }

    public function groupModify($data)
    {
        $res = $this->permissionGroup->find($data['id']);
        if ($res) {
            $res->name = $data['name'];
            $res->status = $data['status'];
            if ($res->force()->save()) {
                return [true, '保存完成'];
            }
        }
        return [false, '未知错误'];
    }

    public function groupDel($id)
    {
        $res = $this->permissionGroup->where('id', $id)->delete();
        return $res ? true : false;
    }

    public function getGroupsToSelect()
    {
        $res = $this->permissionGroup->order('id', 'asc')->select();
        $tmp = [];
        if (! $res->isEmpty()) {
            $res->each(function($item) use (& $tmp) {
                $tmp[] = ['id' => $item['id'], 'name' => $item['name']];
            });
        }
        return $tmp;
    }

    public function getPermissionGroupsToSelect()
    {
        $res = $this->permissionGroup->with('permissions')->order('id', 'asc')->select();
        $tmp = [];
        if (! $res->isEmpty()) {
            return $res->toArray();
        }
        return $tmp;
    }

}
